import sys
import datetime
from pymongo import MongoClient

def GetPVPBattleCountFromSeq() :
	client = MongoClient('172.16.1.192', 27017)
	db = client['pz_user']
	posts = db.log_pvp_play

	start = datetime.datetime(2014, 02, 24, 12)
	end = datetime.datetime(2014, 03, 03, 12)

	print start
	print end

	openfile = file("input.txt", "r")
	writefile = file("output.txt", "w")

	for seq in openfile.readlines() :
        	value = seq.strip()
        	#result = "seq : \t%s\t count : \t%s\n" % (value, posts.find({ "user_seq" : int(value), "reg_time" : { "$gte" : start, "$lt" : end } }).count())
        	#print result	
		#writefile.write(result)

	openfile.close()
	writefile.close()

def GetTotalWinPointFromSeq() :
	client = MongoClient('172.16.1.192', 27017)
	db = client['pz_user']
	posts = db.log_pvp_play

	start = datetime.datetime(2014, 02, 24, 12)
	end = datetime.datetime(2014, 03, 03, 12)

	openfile = file("input.txt", "r")
	writefile = file("output.txt", "w")

	for seq in openfile.readlines() :
                value = seq.strip()
                total = 0
                for result in posts.find({ "user_seq" : int(value), "reg_time" : { "$gte" : start, "$lt" : end } }) :
                        #print result
                        winpoint = result["win_point"];
                        total += int(winpoint)

                out = "seq : \t%s\t winpoint : \t%s \n" % (value, total)	
		print out
		writefile.write(out)
			

	openfile.close()
	writefile.close()

def usage() :
	print " 1: GetPVPBattleCountFromSeq"
	print " 2: GetTotalWinPointFromSeq"
	sys.exit()
	
if __name__ == "__main__" :
	if (len(sys.argv) < 2) : usage()
	
	command = sys.argv[1]

	if ("1" == command) : GetPVPBattleCountFromSeq()
	if ("2" == command) : GetTotalWinPointFromSeq()
	else : usage()
	
